package com.example.microinvestcommon.util;

import java.util.*;

public class StockCodeRandomNumberAssignerUtil {

    private static final String CHARACTERS = "0123456789";
    private static final Random RANDOM = new Random();

    /**
     * 生成随机编号
     * @return 随机编号
     */
    public static String generateRandomCode() {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i <5; i++) {
            sb.append(CHARACTERS.charAt(RANDOM.nextInt(CHARACTERS.length())));
        }
        return sb.toString();
    }

    /**
     * 为股票代码分配唯一随机编号
     * @param stockCodes 股票代码列表
     * @return Map<随机编号, 股票代码>
     */
    public static Map<String, String> assignRandomNumbers(List<String> stockCodes) {
        Map<String, String> result = new LinkedHashMap<>();
        Set<String> usedCodes = new HashSet<>();

        for (String stock : stockCodes) {
            String randomCode;
            do {
                randomCode = generateRandomCode();
            } while (usedCodes.contains(randomCode)); // 保证唯一
            usedCodes.add(randomCode);
            result.put(randomCode, stock);
        }

        return result;
    }
}
